package cn.tj.wenda.cloud.config;


import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import tk.mybatis.mapper.entity.Config;
import tk.mybatis.mapper.mapperhelper.MapperHelper;
import tk.mybatis.spring.mapper.MapperScannerConfigurer;
import tk.mybatis.mapper.code.Style;


@Configuration
//由于MapperScannerConfigurer执行的比较早，所以必须有下面的注解
@AutoConfigureAfter(MyBatisConfiguration.class)
public class MyBatisMapperScannerConfig {

    @Bean()
    public MapperScannerConfigurer mapperScannerConfigurer() {
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();

        //配置通用mappers
        Config config = new Config();
        config.setIDENTITY("MYSQL");// 数据库方言（主要用于：取回主键的方式）
        config.setNotEmpty(false);// insert和update中，是否判断字符串类型!=''，少数方法会用到
        config.setStyle(Style.lowercase);
        config.setSafeDelete(true);
        config.setSafeDelete(true);
        MapperHelper mapperHelper = new MapperHelper();
        mapperHelper.setConfig(config);

        mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");// 多数据源时，必须配置
        mapperScannerConfigurer.setBasePackage("cn.tj.wenda.cloud.*.mapper");//mapper.java文件的路径
        mapperScannerConfigurer.setMapperHelper(mapperHelper);
        return mapperScannerConfigurer;
    }
}
